package com.lgh.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lgh.hotel.dao.mapper.RolesMapper;
import com.lgh.hotel.dao.pojo.Roles;
import com.lgh.hotel.service.RolesService;
import com.lgh.hotel.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class RolesServiceImpl implements RolesService {

    @Autowired
    RolesMapper rolesMapper;

    @Override
    public Result findRolesAll() {
        return Result.succ(rolesMapper.selectList(null));
    }

    @Override
    public Result addRole(String name) {
        Roles roles = new Roles();
        roles.setRoles(name);
        return Result.succ(rolesMapper.insert(roles));
    }

    @Override
    public Result deleteRoles(Integer id) {
        if (id == null){
            return Result.fail(null);
        }
        return Result.succ(rolesMapper.deleteById(id));
    }

    @Override
    public Result updateRoles(Roles roles) {
        return Result.succ(rolesMapper.updateById(roles));
    }

    @Override
    public List<String> findRolesByTypeAll(Integer i) {

        LambdaQueryWrapper<Roles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Roles::getType,i);
        queryWrapper.select(Roles::getRoles);
        List<Roles> roles = rolesMapper.selectList(queryWrapper);

        List<String> strings = rolesToString(roles);

        return strings;
    }

    @Override
    public String findRolesById(String roles) {
        Roles roles1 = rolesMapper.selectById(Integer.parseInt(roles));
        if(roles1 == null){
            return null;
        }
        return roles1.getRoles();
    }

    private List<String> rolesToString(List<Roles> roles) {

        List<String> strings = new ArrayList<>();

        for (Roles role : roles) {
            strings.add(role.getRoles());
        }

        return strings;
    }
}
